/**
 * http://gg1990ok.com,
 * email: guochenguang@163.com
 */

define(['zepto', 'underscore', '../com.52zzb.common/ajax', 'butterflysoc/components/socview', 'butterflysoc/components/store', 'butterflysoc/components/loader',
        'iscroll', '../com.52zzb.common/ajax', '../com.52zzb.common/config',
        'butterflysoc/components/dialog', '../com.52zzb.common/toast', '../com.52zzb.common/swipe', 'async'
    ],
    function($, _, na, SocView, Store, Loader, iScroll, AjaxPlugin, Config, Dialog, Toast, Swipe, Async) {
        var bubble = true; //手动阻止冒泡

        var albumList = [];

        var View = SocView.extend({
            me: null,
            events: {},
            render: function() {
                var me = this;
                $("#home").bind("click", function() {
                    document.removeEventListener("backbutton", onBackKey, false); // 注销返回键 
                    document.addEventListener("backbutton", onDoubleKey, false); //返回键
                    $("#bottom_control").show();
                    $(".main_nav_active").removeClass('main_nav_active');
                    $("#home").addClass('main_nav_active');
                    // window.butterfly.navigate('com.52zzb.main/main', {
                    //     trigger: true
                    // });

                    alert("login-index");

                });
                $("#found").bind("click", function() {

                    window.butterfly.navigate('com.52zzb.main/hot', {
                        trigger: true
                    });

                    $("#bottom_control").show();
                    $(".main_nav_active").removeClass('main_nav_active');
                    $("#found").addClass('main_nav_active');

                });
                $("#notice").bind("click", function() {
                    if (Store.loadObject('user')) {
                        var user = Store.loadObject('user');
                        var uuid = user.uuid;
                        var token = user.token;
                        var deviceId = user.deviceId;
                        var sessionId = user.sessionId;
                        $.ajax({
                            url: Config.server + Config.user_check_login,
                            data: {
                                'uuid': uuid,
                                'token': token,
                                'deviceId': deviceId,
                                'sessionId': sessionId
                            },
                            type: 'post',
                            dataType: 'json',
                            success: function(data) {
                                if (data.code == 10000) {
                                    window.butterfly.navigate('com.52zzb.main/notice', {
                                        trigger: true
                                    });
                                    $("#bottom_control").show();
                                    $(".main_nav_active").removeClass('main_nav_active');
                                    $("#notice").addClass('main_nav_active');

                                } else if (data.code == 10001) {
                                    window.butterfly.navigate('com.52zzb.main/perfect_inf', {
                                        trigger: true
                                    });
                                } else if (data.code == -10001) {
                                    Store.saveObject('href', window.location.href);
                                    window.butterfly.navigate('com.52zzb.main/login', {
                                        trigger: true
                                    });
                                } else {
                                    Toast(data.msg);
                                }
                            }
                        });
                    } else {
                        Store.saveObject('href', window.location.href);
                        window.butterfly.navigate('com.52zzb.main/login', {
                            trigger: true
                        });
                    }
                });
                $("#usercenter_me").bind("click", function() {
                    if (Store.loadObject('user')) {
                        var user = Store.loadObject('user');
                        var uuid = user.uuid;
                        var token = user.token;
                        var deviceId = user.deviceId;
                        var sessionId = user.sessionId;
                        console.log(sessionId);
                        $.ajax({
                            url: Config.server + Config.user_check_login,
                            data: {
                                'uuid': uuid,
                                'token': token,
                                'deviceId': deviceId,
                                'sessionId': sessionId
                            },
                            type: 'post',
                            dataType: 'json',
                            success: function(data) {
                                if (data.code == 10000) {
                                    window.butterfly.navigate('com.52zzb.main/usercenterMe', {
                                        trigger: true
                                    });
                                    $("#bottom_control").show();
                                    $(".main_nav_active").removeClass('main_nav_active');
                                    $("#usercenter_me").addClass('main_nav_active');

                                } else if (data.code == 10001) {
                                    window.butterfly.navigate('com.52zzb.main/perfect_inf', {
                                        trigger: true
                                    });
                                } else if (data.code == -10001) {
                                    Store.saveObject('href', window.location.href);
                                    window.butterfly.navigate('com.52zzb.main/login', {
                                        trigger: true
                                    });
                                } else {
                                    Toast(data.msg);
                                }
                            }
                        });
                    } else {
                        Store.saveObject('href', window.location.href);
                        window.butterfly.navigate('com.52zzb.main/login', {
                            trigger: true
                        });
                    }
                });
                SocView.prototype.render.call(this);
                return this;
            },

            onShow: function() {

                me = this;

                document.body.addEventListener('touchmove', function(event) {
                    event.preventDefault();
                }, false);


                document.removeEventListener("backbutton", onBackKey, false); // 注销返回键 
                document.addEventListener("backbutton", onDoubleKey, false); //返回键
                $("#bottom_control").show();
                $(".main_nav_active").removeClass('main_nav_active');
                $("#home").addClass('main_nav_active');
                // window.butterfly.navigate('com.52zzb.main/main', {
                //     trigger: true
                // });

            }
        });
        return View;
    });
